package br.com.felix.fwt.ui;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import br.com.felix.fwt.error.ApplicationError;
import br.com.felix.fwt.ui.exception.ComponentRenderException;

/**
 * This component prints the ApplicationErrors that occurred in the server side as a HTML unordered list.
 * */
public class ApplicationErrorList extends BaseComponent {
	
	private static final long serialVersionUID = -533907756419522931L;

	/**
	 * List of errors.
	 * */
	private List<ApplicationError> lstErrors;

	/**
	 * Adds an application error to the list.
	 * */
	public void addApplicationError(ApplicationError ae){
		lstErrors.add(ae);
	}
	
	/**
	 * Add all of the application errors to the list.
	 * */
	public void addApplicationErrors(List<ApplicationError> lstVe){
		lstErrors.addAll(lstVe);
	}

	/**
	 * Instantiates an empty ApplicationErrorList.
	 * */
	public ApplicationErrorList(){
		lstErrors = new ArrayList<ApplicationError>();
	}
	
	/**
	 * Instantiates an ApplicationErrorList with the informed errors.
	 * */
	public ApplicationErrorList(List<ApplicationError> lstErrors) {
		this.lstErrors = lstErrors;
	}

	
	/**
	 * Empties the list of Application Errors.
	 * */
	public void clear(){
		lstErrors.clear();
	}



	@Override
	public void write(PrintWriter out) throws ComponentRenderException {
		if(lstErrors!=null && lstErrors.size()>0){
			out.write("<ul style=\"color:red\">");
			for (ApplicationError ae : lstErrors) {
				out.write("<li>");
				out.write(ae.getMessage());
				out.write("</li>");
			}
			out.write("</ul>");
		}
	}
}
